package dice.sinanya.dice.game;

import dice.sinanya.db.coc.SelectCoc;
import dice.sinanya.entity.EntityCocList;
import dice.sinanya.entity.EntityTypeMessages;
import org.nlpcn.commons.lang.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static dice.sinanya.system.MessagesTag.TAG_C_LIST;
import static dice.sinanya.tools.checkdata.CheckIsNumbers.isNumeric;
import static dice.sinanya.tools.makedata.Sender.sender;

public class Clist {
    String msg;
    private final EntityTypeMessages entityTypeMessages;

    public Clist(EntityTypeMessages entityTypeMessages) {
        String tag = TAG_C_LIST;
        this.entityTypeMessages = entityTypeMessages;
        this.msg = entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(tag.substring(0, tag.length() - 2), "").trim();
    }

    public void get() {
        ArrayList<String> wantList = new ArrayList<>(Arrays.asList(msg.split(",")));
        SelectCoc selectCoc = new SelectCoc();
        ArrayList<String> skillValueDefault = new ArrayList<>();
        skillValueDefault.add("魅力");
        skillValueDefault.add("力量");
        skillValueDefault.add("敏捷");
        skillValueDefault.add("意志");
        ArrayList<EntityCocList> cocLists = selectCoc.selectCoc();
        for (EntityCocList cocList : cocLists) {
            int score = cocList.getScore();
            String skill = cocList.getSkill();
            int line = 115;
            for (String want : wantList) {
                if (skill.contains(want)) {
                    score++;
                }
            }
            if (skillValueDefault.contains(wantList.get(0))) {
                if (cocList.getSkillValue().contains("教育*4") || cocList.getSkillValue().contains(wantList.get(0))) {
                    score++;
                }
            }
            if (isNumeric(wantList.get(1))) {
                line = Integer.parseInt(wantList.get(1));
            }
            if (cocList.getId() > line) {
                score -= (229 - line) * 0.025;
            }
            score += cocList.getOthor();
            cocList.setScore(score);
        }
        ArrayList<EntityCocList> result = new ArrayList<>();
        cocLists = sort(cocLists);
        for (int i = 0; i < 20; i++) {
            result.add(cocLists.get(i));
        }
        sender(entityTypeMessages, "根据评分结果，前20个符合你要求的职业分别为:\n评分|职业ID|职业名称|信用区间|技能值算法|技能列表|额外技能数量\n" + StringUtil.joiner(result, "\n"));
    }

    /**
     * 根据value从大到小排序先攻列表
     *
     * @return 排序后的map
     */
    private ArrayList<EntityCocList> sort(ArrayList<EntityCocList> cocLists) {

        List<EntityCocList> list = new ArrayList<>(cocLists);
        //使用Collections工具類對list進行排序
        list.sort((o1, o2) -> {
            //按照age倒敘排列
            int o1Value = o1.getScore();
            int o2Value = o2.getScore();
            return o2Value - o1Value;
        });

        //將list中的數據存入LinkedHashMap中
        return new ArrayList<>(list);
    }

}
